﻿Imports System.Windows.Forms
Imports System.Collections.ObjectModel

Public Class DeviceList
    Public M_PluginList As New Collection
    Private M_PluginSetFile As String
    Private Devlist As New List(Of UCSDevice)
    Private M_Menu_Click As EventHandler
    Private M_Menu As ToolStripMenuItem
    Private M_Tab As TabControl
    Private M_Resources As ResourceManager
    Private Logger As LogFile
    Public Property Menu() As ToolStripMenuItem
        Get
            Return M_Menu
        End Get
        Set(ByVal value As ToolStripMenuItem)
            M_Menu = value
        End Set
    End Property
    Public WriteOnly Property Resources() As ResourceManager
        Set(ByVal value As ResourceManager)
            M_Resources = value
        End Set
    End Property
    Public Property MenuClickEvent() As EventHandler
        Get
            Return M_Menu_Click
        End Get
        Set(ByVal value As EventHandler)
            M_Menu_Click = value
        End Set
    End Property
    Public Function GetDevice(ByVal Name As String) As Control
        Dim P As I_Plugin = GetContainingPlugin(Name)
        If P IsNot Nothing Then
            Dim x As Control = P.GetDevice(Name)
            Return x
        Else
            MsgBox("No Interface : " & Name)
        End If
        Return Nothing
    End Function
    Private Function GetContainingPlugin(ByVal Name As String) As I_Plugin
        For Each P As I_Plugin In M_PluginList
            For Each D As UCSDevice In P.Devices
                If D.Name = Name Then
                    Return P
                End If
            Next
        Next
        Return Nothing
    End Function
    Private Sub InsertMenuItem(ByRef Name As String, ByVal MenuCaption As String, ByVal ReferenceName As String, ByVal Description As String)
        Dim Menu As ToolStripMenuItem
        Dim Cn As Boolean = False

        For Each I As ToolStripMenuItem In M_Menu.DropDownItems
            If I.Text = Name Then Cn = True
        Next

        If Cn = False Then
            Dim Xm As ToolStripMenuItem = M_Menu.DropDownItems.Add(Name)
        End If

        For Each I As ToolStripMenuItem In M_Menu.DropDownItems
            If I.Text = Name Then
                Menu = I
                Dim X As ToolStripMenuItem = Menu.DropDownItems.Add(MenuCaption)
                AddHandler X.Click, M_Menu_Click
                X.Tag = ReferenceName
                X.ToolTipText = Description
                Exit Sub
            End If
        Next
    End Sub
    Private Sub LoadUserDevices()
        Try
            Dim files As ReadOnlyCollection(Of String)
            files = My.Computer.FileSystem.GetFiles(M_Resources.MacroPath, FileIO.SearchOption.SearchAllSubDirectories, "*.Mac")
            For Each F As String In files
                Dim FS As New IO.FileStream(F, IO.FileMode.Open)
                Dim Reader As New IO.StreamReader(FS)
                Dim I As String = Reader.ReadLine
                If I.Contains("[UserDevice]") Then
                    Dim DN As String = Reader.ReadLine.Substring(5)
                    Dim M As String = Reader.ReadLine.Substring(5)
                    InsertMenuItem(M, DN, IO.Path.GetFileName(F), "UserDevice")
                End If
                Reader.Close()
            Next
        Catch


        End Try

    End Sub
    Public ReadOnly Property DeviceCount() As Integer
        Get
            Return M_PluginList.Count
        End Get
    End Property
    Public Sub New()
        M_PluginSetFile = ""
    End Sub

    Public Sub New(ByVal Resources As ResourceManager)
        M_Resources = Resources
        Logger = New LogFile(IO.Path.Combine(M_Resources.AppPath, "DevList.Log"))
    End Sub
    'Public Sub New(ByVal PluginSetFile As String, ByVal Menu As ToolStripMenuItem)
    '   M_PluginSetFile = PluginSetFile
    '   M_Menu = Menu
    'End Sub
    'Public Sub New(ByVal PluginSetFile As String, ByVal Menu As ToolStripMenuItem, ByVal MenuClickEvent As EventHandler)
    '    M_PluginSetFile = PluginSetFile
    '    M_Menu = Menu
    '    M_Menu_Click = MenuClickEvent
    'End Sub
    Public Sub New(ByVal PluginSetFile As String, ByVal Menu As ToolStripMenuItem, ByVal MenuClickEvent As EventHandler, ByVal Resources As ResourceManager)
        M_PluginSetFile = PluginSetFile
        M_Menu = Menu
        M_Menu_Click = MenuClickEvent
        M_Resources = Resources
        Logger = New LogFile(IO.Path.Combine(M_Resources.AppPath, "DevList.Log"))
    End Sub
    Public Sub ScanForPlugins(Optional ByVal Folder As String = "")
        Logger.Write("Scan For Plugins")
        Try
            Dim files As ReadOnlyCollection(Of String)
            If Folder > "" Then
                files = My.Computer.FileSystem.GetFiles(Folder, FileIO.SearchOption.SearchAllSubDirectories, New String() {"*.ucp", "*.dll"})
            Else
                files = My.Computer.FileSystem.GetFiles(M_Resources.PluginPath, FileIO.SearchOption.SearchAllSubDirectories, New String() {"*.ucp", "*.dll"})
            End If

            For Each F As String In files
                Dim PL As I_Plugin = LoadPlugIn(F)
                If PL IsNot Nothing Then
                    M_PluginList.Add(PL)
                    PL.Resources = M_Resources
                End If
            Next
            For Each Plug As I_Plugin In M_PluginList
                For Each D As UCSDevice In Plug.Devices
                    Logger.Write("Device Found : " & D.Caption)
                    Devlist.Add(D)
                Next
            Next
            For Each D As UCSDevice In Devlist
                If D.Visible = True Then
                    InsertMenuItem(D.ParentMenu, D.Caption, D.Name, D.Description)
                End If
            Next
            LoadUserDevices()
        Catch

        End Try
    End Sub

    Public Sub LoadPlugins(Optional ByVal PluginSetFile As String = "")
        If M_Resources.ScanPluginFolder Then
            Dim files As ReadOnlyCollection(Of String)
            files = My.Computer.FileSystem.GetFiles(M_Resources.PluginPath, FileIO.SearchOption.SearchAllSubDirectories, New String() {"*.ucp", "*.dll"})
            For Each F As String In files
                Dim PL As I_Plugin = LoadPlugIn(F)
                If PL IsNot Nothing Then
                    M_PluginList.Add(PL)
                    PL.Resources = M_Resources
                End If
            Next
        Else
            If PluginSetFile > "" Then M_PluginSetFile = PluginSetFile
            If M_PluginSetFile = "" Then Exit Sub
            Dim FS As New IO.FileStream(M_PluginSetFile, IO.FileMode.Open)
            Dim Reader As New IO.StreamReader(FS)
            Do While Reader.EndOfStream = False
                Dim F As String = Reader.ReadLine
                Dim PL As I_Plugin = LoadPlugIn(F)
                If PL IsNot Nothing Then
                    M_PluginList.Add(PL)
                    PL.Resources = M_Resources
                End If
            Loop
            Reader.Close()
        End If

        For Each Plug As I_Plugin In M_PluginList
            For Each D As UCSDevice In Plug.Devices
                Devlist.Add(D)
            Next
        Next
        For Each D As UCSDevice In Devlist
            If D.Visible = True Then InsertMenuItem(D.ParentMenu, D.Caption, D.Name, D.Description)
        Next
        LoadUserDevices()
    End Sub
    Public Sub UnLoadPlugins()
        Devlist.Clear()
        For Each P As I_Plugin In M_PluginList
            P.Unload()
        Next
        M_PluginList.Clear()
        For Each M As ToolStripMenuItem In M_Menu.DropDownItems
            M.DropDownItems.Clear()
        Next
    End Sub
    Private Function LoadPlugIn(ByVal LoadPath As String) As UCBase.I_Plugin
        Logger.Write("Load File : " & LoadPath)
        Dim NewPlugIn As UCBase.I_Plugin
        Try
            Dim PlugInAssembly As Reflection.Assembly = Reflection.Assembly.LoadFrom(LoadPath)
            Logger.Write("Loaded : " & PlugInAssembly.FullName)
            Dim Types() As Type
            Dim FoundInterface As Type
            Types = PlugInAssembly.GetTypes
            For Each PlugInType As Type In Types
                FoundInterface = PlugInType.GetInterface("UCBase.I_Plugin")
                If FoundInterface IsNot Nothing Then
                    NewPlugIn = DirectCast(PlugInAssembly.CreateInstance(PlugInType.FullName), UCBase.I_Plugin)
                    Logger.Write("Plugin Loaded : " & PlugInType.FullName)
                    Return NewPlugIn
                End If
            Next
        Catch ex As Exception
            Logger.Write(ex.Message)
            MsgBox(ex.Message)
        End Try
        Return Nothing
    End Function
End Class
